package cn.liutao.dish.service;


import cn.liutao.common.utils.ServletUtils;
import cn.liutao.dish.entity.po.Cuisine;
import cn.liutao.dish.mapper.CuisineMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import static cn.liutao.dish.entity.po.table.CuisineTableDef.CUISINE;
/**
 * 菜系表 服务层实现。
 *
 * @author liutao
 * @since 2023-10-31
 */
public class CuisineService {
    private static volatile CuisineService instance;
    private CuisineMapper cuisineMapper;
    //私有构造方法
    private CuisineService(){
        cuisineMapper = ServletUtils.getMapper(CuisineMapper.class);
    }


    /**
     * 双重检查锁实现单例
     * @return
     */
    public static CuisineService getInstance(){
        if (instance==null){
            synchronized (CuisineService.class){
                if (instance==null){
                    instance = new CuisineService();
                }
            }
        }
        return instance;
    }


    public Page<Cuisine> selectByList(Cuisine cuisine) {
        Page<Cuisine> paginate = cuisineMapper.paginate(cuisine.getPageNum(), cuisine.getPageSize(),
                QueryWrapper.create()
                .where(Cuisine::getName).like(cuisine.getName())
                .and(CUISINE.STATUS.eq(cuisine.getStatus()))
                .orderBy(CUISINE.SORT,true)
        );
        return paginate;
    }

    public void updateById(Cuisine cuisine) {
        cuisineMapper.update(cuisine);
    }


    public void insert(Cuisine cuisine) {
        cuisineMapper.insertSelective(cuisine);
    }


    public void deleteById(Integer id) {
        cuisineMapper.deleteById(id);
    }
}
